package com.wkck.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wkck.domain.User;
import com.wkck.dto.AdminEditPowerDto;
import com.wkck.dto.ModifyUserPasswordDto;
import com.wkck.dto.QueryUserConditionDto;
import com.wkck.result.Result;
import com.wkck.service.AdminService;
import com.wkck.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Api(tags = "管理业务")
@RestController
@RequestMapping("/manage")
@Slf4j
@CrossOrigin
public class ManageBgController {

    @Autowired
    private AdminService adminService;

    @Autowired
    private UserService userService;

    private static final String default_img = "avatars/default.jpg";

    /**
     * 修改管理员权限
     */
    @PutMapping("/editPower")
    @ApiOperation(value = "修改管理员系统权限")
    public Result<Boolean> editPower(@RequestBody AdminEditPowerDto adminEditPowerDto) {
        log.info("需要修改权限的管理员: {}",adminEditPowerDto);

        return Result.success(adminService.editPower(adminEditPowerDto));
    }

    /**
    * @Author: CK
    * @Date: 2023/11/26
    * @Param: [queryUserConditionDto]
    * @return: 用户集合
    * @Description: 查询用户列表
    */
    @GetMapping("/users")
    @ApiOperation(value = "查询用户列表")
    public Result<IPage<User>> getUserList(QueryUserConditionDto queryUserConditionDto) {
        log.info("模糊查询参数: {}", queryUserConditionDto);
        return Result.success(userService.queryByConditions(queryUserConditionDto));
    }

    /**
    * @Author: CK
    * @Date: 2023/11/27
    * @Param: [id]
    * @return: 是否标识
    * @Description: 根据用户ID删除用户
    */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据用户ID删除用户")
    public Result<Boolean> deleteUserById(@PathVariable Long id) {
        log.info("删除用户,ID: {}" , id);
        return Result.success(userService.removeById(id));
    }

    /**
    * @Author: CK
    * @Date: 2023/11/27
    * @Param: [user]
    * @return: 修改正确与否
    * @Description: 修改用户信息
    */
    @PutMapping
    @ApiOperation(value = "修改用户信息")
    public Result<Boolean> modifyUserInfo(@RequestBody User user) {
        log.info("需要修改的用户信息: {}", user.toString());
        return Result.success(userService.updateById(user));
    }

    /**
    * @Author: CK
    * @Date: 2023/11/27
    * @Param: [user]
    * @return: 保存是否成功
    * @Description: 保存新用户
    */
    @PostMapping
    @ApiOperation(value = "保存新用户")
    public Result<Boolean> saveNewUser(@RequestBody User user) {
        log.info("需要新增的用户信息: {}", user);
        user.setCreate_time(LocalDateTime.now());
        user.setUser_image(default_img);
        return Result.success(userService.save(user));
    }

    /**
    * @Author: CK
    * @Date: 2023/11/27
    * @Param: [modifyUserPasswordDto]
    * @return: 密码修改是否成功
    * @Description: 修改用户密码
    */
    @PostMapping("/modifyPassword")
    @ApiOperation(value = "修改用户密码")
    public Result<Boolean> modifyPassword(@RequestBody ModifyUserPasswordDto modifyUserPasswordDto) {
        log.info("需要修改密码的用户ID: {}", modifyUserPasswordDto.getId());
        return Result.success(userService.modifyUserPassword(modifyUserPasswordDto));
    }
}
